//
//  CYScrollView.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/9/18.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYScrollView.h"
@interface CYScrollView()<UIScrollViewDelegate>{
    CGFloat startX;//scrollview里嵌套scrollview,拦截子scrollview的代理，判断滑动
}

@end
@implementation CYScrollView
//遍历子视图
- (void)ergodicSubView:(void(^)(CYView *view))view{
    NSArray *array = [self subviews];
    for (int i = 0; i < array.count; i++) {
        view(array[i]);
    }
}

- (void)setOffsetY:(CGFloat)offsetY{
    if (_offsetY != offsetY)
        _offsetY = offsetY;
    CGPoint offset = CGPointMake(self.contentOffset.x, offsetY);
    [super setContentOffset:offset animated:YES];
}
- (void)setOffsetX:(CGFloat)offsetX{
    if (_offsetX != offsetX)
        _offsetX = offsetX;
    CGPoint offset = CGPointMake(offsetX,self.contentOffset.y);
    [super setContentOffset:offset animated:YES];
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    startX = scrollView.contentOffset.x;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (startX < scrollView.contentOffset.x) //左滑
        [self setContentOffset:CGPointMake(self.contentOffset.x + scrollView.contentOffset.x - startX, self.contentOffset.y)];
    else if(startX > scrollView.contentOffset.x)//右滑
        [self setContentOffset:CGPointMake(self.contentOffset.x + scrollView.contentOffset.x - startX, self.contentOffset.y)];

    
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
